package com.kwin.excel.filter;

import com.kwin.excel.annotation.ResponseExcel;
import com.kwin.excel.processor.NameProcessor;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.Objects;
import java.util.UUID;

/**
 * @author : wangkai
 * @version V1.0
 * @Package com.kwin.excel.filter
 * @Description:
 * @date 2021年10月19日 10:56
 **/
@Aspect
@RequiredArgsConstructor
public class DynamicNameAspect {
    
    public static final String EXCEL_NAME_KEY = "__EXCEL_NAME_KEY__";
    
    private final NameProcessor processor;
    
    @Before("@annotation(excel)")
    public void around(JoinPoint point, ResponseExcel excel) {
        MethodSignature ms = (MethodSignature) point.getSignature();
        
        String name = excel.name();
        // 当配置的excel 名称为空时，取当前时间
        if (!StringUtils.hasText(name)) {
            name = UUID.randomUUID().toString();
        }
        else {
            name = processor.doDetermineName(point.getArgs(), ms.getMethod(), excel.name());
        }
        
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        Objects.requireNonNull(requestAttributes).
            setAttribute(EXCEL_NAME_KEY, name, RequestAttributes.SCOPE_REQUEST);
    }
    
}
